#!/usr/bin/env python
"""
POST interface for Addama

Provides direct HTTP POST interface to addama-rest.

For more information, please see the documentation in README.txt, or simply
issue help() on any function or module contained within this package.

For license information, please see LICENSE.txt.


Author: Tom Robinson <trobinson@systemsbiology.org>
Initial Revision: April 14th, 2010

"""

# Imports

import cookielib
import os
import sys
from urlparse import urlparse

import addama.api
from config import *


# Function main
#  This is non-standard, and should not be confused with __main__, 
#  which indicates this script is called directly.
def _fmain():
    """Function main: analog for the \"main\" function in other languages""" 
    url = None
    if len(sys.argv) > 1:
        url = sys.argv[1]

    # URL must be supplied on the command line
    if not url:
        raise ValueError

    # Open our cookie jar and import any existing cookies
    authCookies = cookielib.CookieJar()

    if os.path.exists(AUTH_COOKIES):
        authCookies = addama.api.auth.loadCookies(AUTH_COOKIES)

    # Scheme (eg. HTTP, HTTPS), host location, and path must be given
    given = urlparse(url)
    if given.scheme == '' or given.netloc == '' or given.path == '':
        raise ValueError
    
    # Get JSON output from desired resource
    # @todo Eventually, wrap this as a call to a repository object
    print addama.api.resource.postJSON(url, authCookies)


# Initialization and Execution
# Direct invocation
if __name__ == "__main__":
    _fmain()

# Library invocation
# If included as part of a package, the caller is responsible for
# initialization and executing _fmain()
